home *** CD-ROM | disk | FTP | other *** search
/ MacHack 2000 / MacHack 2000.toast / pc / The Hacks / QuitSomethingElse / QuitSomethingElse INIT / INIT.c next >
Encoding:
C/C++ Source or Header  |  2000-06-24  |  3.5 KB  |  132 lines

  1. /* #include files */
  2. #include "A4Stuff.h"
  3. #include "SetupA4.h"
  4. #include <StringCompare.h>
  5.  
  6. /* #defines */
  7. #define kINITid            0
  8.  
  9. #define SEGMENTED        true        /* define if we have a multiple segment INIT */
  10. #define kNumSegments    2            /* total number of segments, including INIT resource itself */
  11.  
  12. #define kQuitDialogId -16552
  13. #define kFinderQuitAlertId 2210
  14. #define kFinderQuitAlertIdSecondary 2216
  15. #define kFinderCreator 'MACS'
  16.  
  17. const Str63 kMyAppName = "\pQuitSomethingElse App";
  18.  
  19. /* some globals local to this file */
  20. FSSpecPtr gLaunchFSSpec;
  21.  
  22. /* Alert patch stuff */
  23. typedef pascal DialogItemIndex (*AlertProc)( SInt16 alertID, ModalFilterUPP modalFilter );
  24. AlertProc    gOldAlertAddr;
  25. pascal DialogItemIndex AlertPatch( SInt16 alertID, ModalFilterUPP modalFilter );
  26.  
  27. /* main */
  28. void main(void)
  29. {
  30.     long    oldA4;
  31.     Handle    h;
  32.     
  33.     /* set up our A4 context for _this file_ */
  34.     oldA4 = SetCurrentA4();
  35.     RememberA4();
  36.     
  37.     /* detach ourselves */
  38.     h = Get1Resource('INIT', kINITid);
  39.     if (h) DetachResource(h);
  40.         
  41.     /* now that we are sure that all segments have been loaded */
  42.     /* (since we called at least one routine in each segment) */
  43.     /* we can continue by detaching each one */
  44. #ifdef SEGMENTED    
  45.     {
  46.         short i;
  47.         for (i=kINITid+1;i<kNumSegments;++i) {
  48.             h = Get1Resource('INIc', i);    /* should NOT fail since each segment should already be loaded and locked */
  49.             if (h) DetachResource(h);
  50.         }
  51.     }
  52. #endif
  53.  
  54.     /* patch Alert */
  55.     gOldAlertAddr = (AlertProc)GetToolTrapAddress(_Alert);
  56.     SetToolTrapAddress((UniversalProcPtr)AlertPatch, _Alert);
  57.     
  58. cleanup:
  59.     /* restore the a4 world */
  60.     SetA4(oldA4);
  61. }
  62.  
  63. /* AlertPatch */
  64. pascal DialogItemIndex AlertPatch( SInt16 alertID, ModalFilterUPP modalFilter )
  65. {
  66.     long oldA4;
  67.     DialogItemIndex itemHit = -1;
  68.     ProcessSerialNumber thePSN;
  69.     ProcessInfoRec info;
  70.     OSErr theError;
  71.     FSSpec processFSSpec, launchFSSpec;
  72.     Boolean    result = false;
  73.     DialogItemIndex theDialogItemIndex = -1;
  74.     LaunchParamBlockRec myLaunchParams;
  75.     long foundDirID;
  76.     short foundVRefNum;
  77.     short j;
  78.     AppParameters appToLaunch;
  79.     
  80.     /* use the global data in _this file_ */
  81.     oldA4 = SetUpA4();
  82.  
  83.     // Look for the Finder.
  84.     thePSN.lowLongOfPSN = kNoProcess;
  85.     thePSN.highLongOfPSN = 0;
  86.     
  87.     info.processInfoLength = sizeof( FSSpec );
  88.     info.processName = NULL;
  89.     info.processAppSpec = &processFSSpec;
  90.     
  91.     theError = GetFrontProcess( &thePSN );
  92.     
  93.     if ( noErr == GetProcessInformation( &thePSN, &info ) ) {
  94. //        DebugStr( "\pChecking front process." );
  95.         if ( ( alertID == kFinderQuitAlertId || alertID == kFinderQuitAlertIdSecondary ) && info.processSignature == kFinderCreator ) {
  96. //            DebugStr( "\pFound the Finder!" );
  97.             
  98.             theError = FindFolder( -1, kSystemFolderType, false, &foundVRefNum, &foundDirID );
  99.             if ( theError == noErr ) {
  100.                 launchFSSpec.vRefNum = foundVRefNum;
  101.                 launchFSSpec.parID = foundDirID;
  102.  
  103.                 for ( j = 0; j <= kMyAppName[ 0 ]; j++ )
  104.                     launchFSSpec.name[ j ] = kMyAppName[ j ];
  105.  
  106.                 myLaunchParams.launchBlockID = extendedBlock;
  107.                 myLaunchParams.launchEPBLength = extendedBlockLen;
  108.                 myLaunchParams.launchFileFlags = 0;
  109.                 myLaunchParams.launchControlFlags = launchContinue + launchNoFileFlags;
  110.                 myLaunchParams.launchAppSpec = &launchFSSpec;
  111.                 myLaunchParams.launchAppParameters = &appToLaunch;
  112.                 appToLaunch.eventRefCon = ( unsigned long )&gLaunchFSSpec;
  113.                 
  114.                 theError = LaunchApplication( &myLaunchParams );
  115.  
  116.                 if ( theError == noErr )
  117.                     result = true;
  118.             }
  119.         }
  120.     }
  121.  
  122.     /* call through to the original Alert */
  123.     if ( !result )
  124.         theDialogItemIndex = gOldAlertAddr( alertID, modalFilter );
  125.     
  126.     /* restore the a4 world */
  127.     RestoreA4(oldA4);
  128.     
  129.     return theDialogItemIndex;
  130. }
  131.  
  132.